//
//  SuperDialogController.m
//  MyCloudMusic
//
//  Created by pub on 2024/6/15.
//

#import "SuperDialogController.h"

@interface SuperDialogController ()<QMUIModalPresentationContentViewControllerProtocol>

@end

@implementation SuperDialogController

- (void)initViews{
    [super initViews];
    
    self.view.backgroundColor = [UIColor colorDivider];
    self.view.myWidth = MyLayoutSize.fill;
    self.view.myHeight = MyLayoutSize.wrap;
    
    [self.view smallRadius];
    
    
    self.superRootContainer = [[MyLinearLayout alloc] initWithOrientation:MyOrientation_Vert];
    self.superRootContainer.myWidth = MyLayoutSize.fill;
    self.superRootContainer.myHeight = MyLayoutSize.wrap;
    self.superRootContainer.subviewSpace = 0.5;
    [self.view addSubview:self.superRootContainer];
    
    self.superContentContainer = [[MyLinearLayout alloc] initWithOrientation:MyOrientation_Vert];
    self.superContentContainer.myWidth = MyLayoutSize.fill;
    self.superContentContainer.myHeight = MyLayoutSize.wrap;
    self.superContentContainer.subviewSpace = 0.5;
    [self.superRootContainer addSubview:self.superContentContainer];
    
    [self.superContentContainer addSubview:self.titleView];
    
    
    self.superFooterContainer = [[MyLinearLayout alloc] initWithOrientation:MyOrientation_Horz];
    self.superFooterContainer.myWidth = MyLayoutSize.fill;
    self.superFooterContainer.myHeight = MyLayoutSize.wrap;
    self.superFooterContainer.subviewSpace = 0.5;
    [self.superRootContainer addSubview:self.superFooterContainer];
    
    for (QMUIButton *it in self.buttons) {
        [self.superFooterContainer addSubview:it];
    }
}


- (NSMutableArray *)buttons{
    if(!_buttons){
        _buttons = [[NSMutableArray alloc] init];
    }
    return _buttons;
}

- (UILabel *)titleView{
    if (!_titleView) {
        _titleView = [UILabel new];
        _titleView.myWidth = MyLayoutSize.fill;
        _titleView.myHeight = BUTTON_MEDDLE;
        _titleView.text = @"标题";
        _titleView.textAlignment = NSTextAlignmentCenter;
        _titleView.font = [UIFont systemFontOfSize:TEXT_LARGE3];
        _titleView.textColor = [UIColor black32];
    }
    return _titleView;
}

- (SuperDialogController *)setConfirmButtom:(NSString *)title target:(id)target action:(SEL)action{
    [self addButton:title color:[UIColor primaryColor] target:target action:action];
    return self;
}

- (SuperDialogController *)setWarningButtom:(NSString *)title target:(id)target action:(SEL)action{
    [self addButton:title color:[UIColor primaryColor] target:target action:action];
    return self;
}

- (SuperDialogController *)setCancelButtom:(NSString *)title target:(id)target action:(SEL)action{
    [self addButton:title color:[UIColor primaryColor] target:target action:action];
    return self;
}
- (SuperDialogController *)addButton:(NSString *)title color:(UIColor *)color target:(id)target action:(SEL)action{
    QMUIButton *result = [ViewFactoryUtil title:title color:color];
    
    if(action){
        [result addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    }
    else{
        [result addTarget:target action:@selector(onCancle:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    result.myWidth = MyLayoutSize.wrap;
    result.myHeight = BUTTON_LARGE;
    result.weight = 1;
    
    [self.buttons addObject:result];
    return self;
}

- (void)onCancle:(UIButton *)btn{
    [self hide];
}

- (void)show{
    if(self.modalController.visible){
        return;
    }
    
    self.modalController = [QMUIModalPresentationViewController new];
    
    self.modalController.animationStyle = QMUIModalPresentationAnimationStyleFade;
    
    [self.modalController setModal:YES];
    
    self.modalController.contentViewMargins = UIEdgeInsetsMake(PADDING_LARGE2, PADDING_LARGE2, PADDING_LARGE2, PADDING_LARGE2);
    
    self.modalController.contentViewController = self;
    
    [self.modalController showWithAnimated:YES completion:nil];
}

- (void)hide{
    [self.modalController hideWithAnimated:YES completion:nil];
}

@end
